<?php
// 会员管理，可CURD操作会员，
class CustomerAction extends CommonAction {
    function _filter(&$map) {
         $key = trim($_REQUEST['keywords']);
         if(!empty($key)) {
             $map['name'] = array('like',"%".$key."%");
             $map['company'] = array('like',"%".$key."%");
             $map['email'] = array('like',"%".$key."%");
             $map['phone'] = array('like',"%".$key."%");
             $map['company'] = array('like',"%".$key."%");
             $map['address'] = array('like',"%".$key."%");
             $map['_logic'] = "or";
             //$map['profile'] =$key; //保证分页跳转正常,注意不要查询profile
         }
    }
    

    function _before_add(){
        $this->assign('type',C('customer_type'));
    }

     function _before_edit(){
        $this->assign('type',C('customer_type'));
    }

    function _before_index(){
        $this->assign('type',C('customer_type'));
    }

    public function insert(){
        
        //B('FilterString');
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
             $mod_field = getModelName ($list, $name,$field);
            //如果未取到字段值 ，则返回对应的ID编号
            $msg = '新增 '.$name.': '.$mod_field.' (编号：'.$list.')';
            if('Action' != $name)
                A('Action')->writeLog($msg,'新增',$name,$list);

            if(!empty($_REQUEST['jumpUrl'])){
                $jumpUrl = '__APP__/'.$_REQUEST['jumpUrl'].'/add/customer_id/'.$list;
            }

            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl',  $jumpUrl);
            $this->success ('新增成功!');
        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }

    //修改供应商等级
    public function edit_rank(){
       $this->assign('type',C('customer_type'));
       $this->edit('edit_rank'); 
    }


    // 检查帐号
    public function checkAccount() {
        /*
        if(!preg_match('/^[a-z]\w{4,}$/i',$_POST['account'])) {
            $this->error( '用户名必须是字母，且5位以上！');
        }*/
        $Member = M("Member");
        // 检测用户名是否冲突
        $name  =  $_REQUEST['account'];
        $result  =  $Member->getByAccount($name);
        if($result) {
            $this->error('该用户名已经存在！');
        }else {
            $this->success('该用户名可以使用！');
        }
    }


    //重置密码
    public function resetPwd() {
        $id  =  $_POST['id'];
        $password = $_POST['password'];
        if(''== trim($password)) {
            $this->error('密码不能为空！');
        }
        $User = M('Member');
        $User->password	=	md5($password);
        $User->id			=	$id;
        $result	=	$User->save();
        if(false !== $result) {
            $this->success("密码修改为$password");
        }else {
            $this->error('重置密码失败！');
        }
    }

    public function foreverdelete() {
        //删除指定记录
        $model = D ('Customer');
        if (! empty ( $model )) {
            $pk = $model->getPk ();
            $id = $_REQUEST [$pk];
            if (isset ( $id )) {
                $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
                //如果会员存在入住或预订，或留言记录，则不能删除
                $ids = explode(",", $id);
                foreach($ids as $member_id){
                    if($this->_exist_record($member_id)){
                        $this->error ('会员:'.getModelName($member_id, 'Customer').' 存在历史记录，暂不能删除！');
                        exit;
                    }
                }
                parent::foreverdelete();
            } else {
                $this->error ( '非法操作' );
            }
        }
        $this->forward ();
    }

    //查找该会员是否存在相关交易的记录，如是，返回true,否则返回false
    protected function _exist_record($member_id){
        /*$count_order = count_order($member_id);
        $count_checkin = count_checkin($member_id);
         $count_msg = count_msg($member_id);
         if( ($count_checkin+$count_msg+$count_order) > 0){
             return true;
         }else{
             return false;
         }
*/
//TODO
        return fale;
    }

}
?>
